package com.hy.frame.util;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

public class ActUtil {
    public static final int REQUEST_NO_CODE = -1;

    public static void startAct(Context cxt, Class<?> cls) {
        startAct(cxt, cls, null);
    }

    public static void startAct(Context cxt, Class<?> cls, Bundle bundle) {
        startAct(cxt, cls, bundle, null);
    }

    public static void startAct(Context cxt, Class<?> cls, Bundle bundle, Intent intent) {
        startActForResult(cxt, cls, REQUEST_NO_CODE, bundle, intent);
    }

    public static void startActForResult(Context cxt, Class<?> cls, int requestCode) {
        startActForResult(cxt, cls, requestCode, null);
    }

    public static void startActForResult(Context cxt, Class<?> cls, int requestCode, Bundle bundle) {
        startActForResult(cxt, cls, requestCode, bundle, null);
    }

    public static void startActForResult(Context cxt, Class<?> cls, int requestCode, Bundle bundle, Intent intent) {
        Intent i = intent;
        if (i == null)
            i = new Intent();
        if (bundle != null)
            i.putExtras(bundle);
        i.setClass(cxt, cls);
        if (requestCode != REQUEST_NO_CODE && cxt instanceof android.app.Activity)
            ((android.app.Activity) cxt).startActivityForResult(i, requestCode);
        else
            cxt.startActivity(i);
    }

    public static void startAct(Fragment cxt, Class<?> cls) {
        startAct(cxt, cls, null);
    }

    public static void startAct(Fragment cxt, Class<?> cls, Bundle bundle) {
        startAct(cxt, cls, bundle, null);
    }

    public static void startAct(Fragment cxt, Class<?> cls, Bundle bundle, Intent intent) {
        startActForResult(cxt, cls, REQUEST_NO_CODE, bundle, intent);
    }

    public static void startActForResult(Fragment fgm, Class<?> cls, int requestCode) {
        startActForResult(fgm, cls, requestCode, null);
    }

    public static void startActForResult(Fragment fgm, Class<?> cls, int requestCode, Bundle bundle) {
        startActForResult(fgm, cls, requestCode, bundle, null);
    }

    public static void startActForResult(Fragment fgm, Class<?> cls, int requestCode, Bundle bundle, Intent intent) {
        if (fgm.getContext() == null) return;
        Intent i = intent;
        if (i == null)
            i = new Intent();
        if (bundle != null)
            i.putExtras(bundle);
        i.setClass(fgm.getContext(), cls);
        if (requestCode != REQUEST_NO_CODE)
            fgm.startActivityForResult(i, requestCode);
        else
            fgm.startActivity(i);
    }
}
